<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="用户UID">
          <el-input v-model="form.uid" placeholder="用户UID" />
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.mobile" placeholder="手机号" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" placeholder="邮箱" />
        </el-form-item>
        <el-form-item label="邀请码">
          <el-input v-model="form.invite_code" placeholder="邀请码" />
        </el-form-item>
        <el-form-item label="注册方式">
          <el-select v-model="form.login_way" placeholder="注册方式">
            <el-option label="mobile" value="mobile" />
            <el-option label="email" value="email" />
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期" :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center"/>
          <el-table-column prop="uid" label="用户UID" align="center" width="120"/>
          <el-table-column prop="nickname" label="昵称" align="center"/>
          <el-table-column prop="login_way" label="注册方式" align="center"/>
          <el-table-column prop="account_name" label="账号" align="center" width="200"/>
          <el-table-column prop="mobile_area_code" label="区号" align="center" />
          <el-table-column prop="bind_mobile" label="手机号" align="center" />
          <el-table-column prop="invite_code" label="邀请码" align="center" width="150" />
          <el-table-column prop="is_certification" label="是否实名" align="center" :formatter="formatterCertification"/>
          <el-table-column prop="is_delete" label="是否删除" align="center" :formatter="formatterCertification"/>
          <el-table-column prop="is_banned" label="是否封禁" align="center"  :formatter="formatterCertification"/>
          <el-table-column
            prop="createtime"
            label="注册时间"
            align="center"
            :formatter="formatterTime"
            width="180"
          />
          <el-table-column prop="google_uuid" label="谷歌ID" align="center"/>
          <el-table-column prop="apple_uuid" label="苹果ID" align="center"/>
          <el-table-column prop="twitter_uuid" label="推特ID" align="center"/>
          <el-table-column prop="outside_id" label="三方标识" align="center"/>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, exportToExcel,timestamp, pickerDefaultTime } from "@/utils/utils";
import { userListApi } from "@/api/slotsgame/user/user";

const loading = ref(false);
const tableData = ref([]);
const formData = {
  timeData: [],
  uid: "",
  mobile: "",
  email:"",
  login_way:"",
  invite_code: ""
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    uid: form.uid,
    mobile: form.mobile,
    email: form.email,
    login_way: form.login_way,
    invite_code: form.invite_code,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};

const formatterCertification = (row, column, cellValue, index) => {
  let obj = {
    0: "否",
    1: "是",
    2: "审核中",
  };
  return obj[cellValue];
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await userListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await userListApi(data);

  let head = {
    uid: "用户id",
    id: "自增id",
    nickname: "用户昵称",
    account_name: "账号",
    mobile_area_code: "	手机号区号",
    bind_mobile: "手机号",
    login_way: "注册方式",
    is_certification: "是否实名",
    is_delete: "是否删除",
    is_banned: "是否封禁",
    google_uuid: "谷歌ID",
    apple_uuid: "苹果ID",
    twitter_uuid: "推特ID",
    outside_id: "三方标识",
    createtime: "注册时间",
  };
  exportToExcel(res.data.results, head, "Slotsgame-用户信息");
};
// 条数
const handleSizeChange = number => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = number => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
